package com.zhouyu;

import com.zhouyu.service.UserService;
import org.springframework.cglib.proxy.*;

import java.lang.reflect.Method;

public class CglibDemo {
	public static void main(String[] args) {
		UserService target = new UserService();
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(UserService.class);
		enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
			@Override
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
				System.out.println("before...");
				Object result = methodProxy.invoke(target, objects);
				//Object result = method.invoke(target, objects);
				System.out.println("after...");
				return result;
			}
		}, NoOp.INSTANCE});
		enhancer.setCallbackFilter(new CallbackFilter() {
			@Override
			public int accept(Method method) {
				if(method.getName().equals("test")) {
					// 执行第一个下标的拦截器
					return 0;
				}else {
					return 1;
				}
			}
		});
		UserService o = (UserService)enhancer.create();
		o.test();
	}
}
